import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.*;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RefineryUtilities;

public class FrameChart extends JFrame
{
    private DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    private JFreeChart chart;
    FrameChart(String chart_title, String absciss_label, String ordinate_label)
    {
        setTitle("Chart");     
        setSize( 640, 480 );
        RefineryUtilities.centerFrameOnScreen(this);
        setVisible(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);   
        startChart(chart_title, absciss_label, ordinate_label);
    }
    private void startChart(String chart_title, String absciss_label, String ordinate_label)
    {
        chart = ChartFactory.createLineChart(
            chart_title,                      // chart title
            absciss_label,                    // domain axis label
            ordinate_label,                   // range axis label
            dataset,                          // data
            PlotOrientation.VERTICAL,        // orientation
            true,                             // include legend
            true,                             // tooltips
            false                             // urls
        );
        chart.setBackgroundPaint(Color.white);
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.white);
        
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setAutoRangeIncludesZero(true);
        
        LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
        renderer.setDrawOutlines(true);
        for(int i = 0; i < 7; i++)
        {
            renderer.setSeriesStroke(
                i, new BasicStroke(
                3.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                1.0f, new float[] {1.0f, 10.0f}, 0.0f
                )
            );
        }
        this.getContentPane().add(new ChartPanel(chart), BorderLayout.CENTER );
        JPanel panel = new JPanel();
        JButton button_get_statistic = new JButton("Get Statistic");
        JButton button_save_chart = new JButton("Save Chart");
        button_get_statistic.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonGetStatisticActionPerformed(evt);
            }
        });
        button_save_chart.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSaveChartActionPerformed(evt);
            }
        });
        panel.add(button_get_statistic);
        panel.add(button_save_chart);
        this.getContentPane().add(panel, BorderLayout.SOUTH);
    }
    public void buttonGetStatisticActionPerformed(ActionEvent evt)
    {
        FrameStatistic statistic_frame = new FrameStatistic();      
        statistic_frame.runStatistic();
        ArrayList<ArrayList> statistic_row = CoordinateAxis.getStatistic();
        Iterator statistic_row_iterator = statistic_row.iterator();
        while(statistic_row_iterator.hasNext())
        {
            ArrayList<Object> values = (ArrayList<Object>)statistic_row_iterator.next();
            statistic_frame.addRowToTable(values);
        }
    }
    public void buttonSaveChartActionPerformed(ActionEvent evt)
    {
        SaveChart(chart.getTitle().getText());
    }
    public void addLine(String line_name, List columns, List data) 
    {
        Iterator data_iter = data.iterator();
        Iterator col_iter = columns.iterator();
        for(int i = 0 ; data_iter.hasNext() && col_iter.hasNext(); i++)
        {
            String column_name = (String)col_iter.next();
            Number point = (Number)data_iter.next();
            dataset.addValue(point, line_name, column_name);
        }     
    }    
    public void SaveChart(String file_name)
    {
        try 
        {
            File file = new File("/home/tatyana/work/TDiplom/Chart/" + file_name + ".jpg");
            ChartUtilities.saveChartAsJPEG(file, chart, 1024, 768);
        } 
        catch (Exception e) 
        {
            System.out.println("Problem occurred creating chart.");
        }
    }
}

